package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.entity.*;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.CMSService;
import com.yenlien.traceyun.control.service.rest.CompanyRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/cms")
@Api(tags = "果园CMS")
public class CMSController extends BaseController {

    @Autowired
    private CMSService cmsService;
    @Autowired
    private CompanyRestService companyRestService;


    @PostMapping(value = "/saveExpert")
    @ApiOperation(value = "保存专家信息", notes = "headImg为图片上传后的名字(图片先上传)。有Id则代表修改，没有则是添加")
    public Object saveExpert(@RequestBody Expert expert)  {
        return success(cmsService.saveExpert(expert));
    }
    @GetMapping(value = "/findAllExpert")
    @ApiOperation(value = "查询所有专家信息")
    public Object findAllExpert()  {
        return success(cmsService.findAllExpert());
    }
    @GetMapping(value = "/delExpertById")
    @ApiOperation(value = "根据ID删除专家信息", notes = "参数: id")
    public Object delExpertById(String id)  {
        cmsService.delExpertById(id);
        return success("success");
    }
    @PostMapping(value = "/findExpertPage")
    @ApiOperation(value = "分页查询专家信息", notes = "可选参数: type 分页: page, pageNum(页的数量)")
    public Object findExpertPage(@RequestBody Expert expert)  {
        return success(cmsService.findExpertPage(expert));
    }
    @PostMapping(value = "/findExpertList")
    @ApiOperation(value = "参数查询专家信息不分页", notes = "可选参数: type")
    public Object findExpertList(@RequestBody Expert expert)  {
        return success(cmsService.findExpertList(expert));
    }


    @GetMapping(value = "/findAllFruitTownExpert")
    @ApiOperation(value = "查询所有果园镇信息和专家信息以及镇信息")
    public Object findAllFruitTownExpert()  {
        return success(cmsService.findAllFruitTownExpert());
    }


    @PostMapping(value = "/saveFruitTown")
    @ApiOperation(value = "保存果园各镇信息")
    public Object saveFruitTown(@RequestBody FruitTown fruitTown)  {
        return success(cmsService.saveFruitTown1(fruitTown));
    }
    @GetMapping(value = "/findAllFruitTown")
    @ApiOperation(value = "查询所有果园镇信息")
    public Object findAllFruitTown()  {
        return success(cmsService.findAllFruitTown());
    }

    @GetMapping(value = "/findFruitTownById")
    @ApiOperation(value = "根据Id查询镇/村信息")
    public Object findFruitTownById(String id)  {
        return success(cmsService.findFruitTownById(id));
    }


    @GetMapping(value = "/delFruitTownById")
    @ApiOperation(value = "根据ID删除果园镇信息,如果是镇则删除下属村级", notes = "参数: id")
    public Object delFruitTownById(String id)  {
        cmsService.delFruitTownById(id);
        return success("success");
    }
    @PostMapping(value = "/findFruitTownPage")
    @ApiOperation(value = "分页查询果园镇信息", notes = "可选参数: name, type 分页: page, pageNum(页的数量)")
    public Object findFruitTownPage(@RequestBody FruitTown fruitTown)  {
        return success(cmsService.findFruitTownPage(fruitTown));
    }
    @PostMapping(value = "/findFruitTownList")
    @ApiOperation(value = "参数查询果园镇信息不分页", notes = "可选参数: name(等值匹配), type")
    public Object findFruitTownList(@RequestBody FruitTown fruitTown)  {
        return success(cmsService.findFruitTownList(fruitTown));
    }
    @GetMapping(value = "/findFruitTownByNameType")
    @ApiOperation(value = "根据 name(等值匹配) 和 type 查询", notes = "")
    public Object findByNameType(String name, String type)  {
        return success(cmsService.findByNameType(name, type));
    }







    @PostMapping(value = "/saveBanner")
    @ApiOperation(value = "保存Banner", notes = "img为图片上传后的名字(图片先上传)。有Id则代表修改，没有则是添加")
    public Object save(@RequestBody FruitBanner fruitBanner)  {
        return success(cmsService.saveBanner(fruitBanner));
    }

    @GetMapping(value = "/delBannerById")
    @ApiOperation(value = "根据ID删除Banner", notes = "参数: id")
    public Object delBannerById(String id)  {
        cmsService.delBannerById(id);
        return success("success");
    }




    @PostMapping(value = "/saveRecall")
    @ApiOperation(value = "保存召回信息", notes = "img为图片上传后的名字(图片先上传)。有Id则代表修改，没有则是添加")
    public Object saveRecall(@RequestBody FruitRecall fruitRecall)  {
        return success(cmsService.saveRecall(fruitRecall));
    }



    @PostMapping(value = "/saveArticle")
    @ApiOperation(value = "保存文章", notes = "img为图片上传后的名字(图片先上传)。有Id则代表修改，没有则是添加")
    public Object saveArticle(@RequestBody Article article)  {
        return success(cmsService.saveArticle(article));
    }



    @PostMapping(value = "/saveComp")
    @ApiOperation(value = "保存名企名品", notes = "原有数据原封不动传递")
    public Object saveComp(@RequestBody JSONObject jsonObject)  {
        return success(companyRestService.updateInfo(jsonObject));
    }


    @GetMapping(value = "/state")
    @ApiOperation(value = "修改文章状态", notes = "参数：id=文章ID, state= 1(未发布), 2(已发布)")
    public Object state(@RequestParam String id, @RequestParam(defaultValue = "2") String state)  {
        return success(cmsService.state(id, state));
    }


    @GetMapping(value = "/findArticleDown")
    @ApiOperation(value = "获取已发布的文章下拉列表", notes = "返回值包含ID和名称, 可选参数: companyId (未传则使用当前账号所属company)")
    public Object findArticleDown(String companyId)  {
        return success(cmsService.findArticleDown(companyId));
    }




    @PostMapping(value = "/findContentList")
    @ApiOperation(value = "分页查询文章", notes = "可选参数: type, state, homeShow(是否首页展示), hotspot(是否是热点文章), publisher， 分页: page, pageNum(页的数量)")
    public Object findContentList(@RequestBody Article article)  {
        article.setPageType("1");
        return success(cmsService.getContentList(article));
    }





    // 后台接口
    @PostMapping(value = "/findBannerList")
    @ApiOperation(value = "分页查询Banner", notes = "可选参数: type")
    public Object findBannerList(@RequestBody FruitBanner fruitBanner)  {
        return success(cmsService.findBannerPage(fruitBanner));
    }


    @GetMapping(value = "/findArticleById")
    @ApiOperation(value = "根据ID查看文章", notes = "参数： id")
    public Object findArticleById(String id)  {
        return success(cmsService.findArticleById(id));
    }

    @GetMapping(value = "/findRecallById")
    @ApiOperation(value = "根据ID查看召回", notes = "参数： id")
    public Object findRecallById(String id)  {
        return success(cmsService.findRecallById(id));
    }




    @PostMapping(value = "/findRecallList")
    @ApiOperation(value = "分页查询召回信息", notes = "可选参数: productName(模糊匹配), batch , 分页: page, pageNum")
    public Object findRecallList(@RequestBody FruitRecall fruitRecall)  {
        return success(cmsService.findRecallList(fruitRecall));
    }

//    @PostMapping(value = "/findContentList")
//    @ApiOperation(value = "分页查询文章", notes = "可选参数: type, state, homeShow, hotspot, publisher")
//    public Object findContentList(@RequestBody Article article)  {
//        return success(cmsService.findContentList(article));
//    }


    @PostMapping(value = "/findCompList")
    @ApiOperation(value = "名企名品查询", notes = "")
    public Object findCompList(@RequestBody JSONObject jsonObject)  {
        jsonObject.put("id", UserAuthUtil.getLoginUser().getCompanyId());
        return companyRestService.findFruitCompList(jsonObject);
    }
    @PostMapping(value = "/findAllFruitCompList")
    @ApiOperation(value = "查询所有名企名品", notes = "")
    public Object findAllFruitCompList()  {
        return companyRestService.findAllFruitCompList();
    }

    // 名企详情
    @GetMapping(value = "/findCompById")
    @ApiOperation(value = "根据ID查询名企名品", notes = "参数: companyId")
    public Object findById(String companyId) {
        return companyRestService.findById(companyId);
    }



    @PostMapping(value = "/getBannerList")
    @ApiOperation(value = "查询Banner", notes = "可选参数: type")
    public Object getBannerList(@RequestBody FruitBanner fruitBanner)  {
        return success(cmsService.findBannerList(fruitBanner));
    }

    @PostMapping(value = "/getContentList")
    @ApiOperation(value = "条件查询文章, 携带分页参数 pageType 则分页", notes = "可选参数: type, state, homeShow(是否首页展示), hotspot(是否是热点文章), publisher， 分页: page, pageNum, pageType=1代表分页")
    public Object getContentList(@RequestBody Article article)  {
        return success(cmsService.getContentList(article));
    }




}
